import requests


def generate_osm_map_link(region):
    return f"https://www.openstreetmap.org/search?query={region.name}"


def generate_bounding_box(region):
    lat1, lon1 = region.latitude, region.longitude
    lat2, lon2 = region.latitude_2, region.longitude_2

    min_lat = min(lat1, lat2)
    max_lat = max(lat1, lat2)
    min_lon = min(lon1, lon2)
    max_lon = max(lon1, lon2)

    top_left = [max_lat, min_lon]
    top_right = [max_lat, max_lon]
    bottom_right = [min_lat, max_lon]
    bottom_left = [min_lat, min_lon]

    return [top_left, top_right, bottom_right, bottom_left]


def get_pois(latitude, longitude, radius=100):
    overpass_url = "http://overpass-api.de/api/interpreter"
    overpass_query = f"""
        [out:json];
        (
            node["amenity"](around:{radius},{latitude},{longitude});
            way["amenity"](around:{radius},{latitude},{longitude});
            relation["amenity"](around:{radius},{latitude},{longitude});
        );
        out center;
    """
    response = requests.get(overpass_url, params={'data': overpass_query})
    data = response.json()

    resp = []

    for element in data['elements']:
        poi_name = element.get('tags', {}).get('name', 'Unnamed')
        poi_type = element.get('tags', {}).get('amenity', 'Unknown')
        poi_latitude = element.get('lat')
        poi_longitude = element.get('lon')
        if poi_longitude and poi_latitude:
            resp.append({
                "name": poi_name,
                "type": poi_type,
                "latitude": poi_latitude,
                "longitude": poi_longitude
            })

    return resp
